<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Base\Charge;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

/**
 * 渠道管理.
 */
class ChannelController extends Controller
{
    use ModelForm;

    /**
     * [index 首页列表].
     *
     * @Author   王文凡
     * @DateTime 2018-04-02
     *
     * @version  1.0
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.top_up'));
            $content->description(trans('game.channel'));
            $content->body($this->grid()->render());
        });
    }

    protected function grid()
    {
        $grid = Admin::grid(Charge::class, function (Grid $grid) {
            $grid->id('ID');
            $grid->name(trans('game.name'));
            $grid->channel_no(trans('game.number'));
            $grid->app_key(trans('game.pay_key'))->display(function ($content) {
                return $content ? str_limit($content, 30, '...') : '';
            });
            $grid->pname()->name(trans('game.superior_channel'));
            $grid->status('状态')->display(function ($status) {
                return $status ? trans('game.normal') : trans('game.lock');
            });
            $grid->disableFilter();
        });

        return $grid;
    }

    /**
     * [create 新增渠道].
     *
     * @Author   王文凡
     * @DateTime 2018-03-24
     *
     * @version  1.0
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.top_up'));
            $content->description(trans('admin.new'));
            $content->body($this->form());
        });
    }

    protected function form()
    {
        return Admin::form(Charge::class, function (Form $form) {
            $collection = Charge::where('pid', 0)->get()->pluck('name', 'id')->all();
            $form->text('name', trans('game.name'))->rules('required');
            $form->text('channel_no', trans('game.number'))->rules('required');
            $collection[0] = trans('game.first_channel');
            $form->select('pid', trans('game.superior_channel'))->options($collection)->rules('required');
            $form->text('app_key', trans('game.pay_key'));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('game.top_up'));
            $content->description(trans('admin.edit'));
            $content->body($this->form()->edit($id));
        });
    }
}
